<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Payment_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function offline_payment(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "create_date >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "create_date <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('status','pending')->get(TBL_OFFLINE_PAYMENT)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_OFFLINE_PAYMENT." WHERE $cond AND status LIKE 'pending'")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/offline_payment/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond) {
			$records = $this->db->where('status','pending')->order_by('create_date','desc')->limit($records_per_page, $per_page )->get(TBL_OFFLINE_PAYMENT)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_OFFLINE_PAYMENT." WHERE {$cond} AND status LIKE 'pending' ORDER BY create_date DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function offline_payment_approved(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "create_date >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "create_date <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('status','approved')->get(TBL_OFFLINE_PAYMENT)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_OFFLINE_PAYMENT." WHERE $cond AND status LIKE 'approved'")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/offline_payment2/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond) {
			$records = $this->db->where('status','approved')->order_by('create_date','desc')->limit($records_per_page, $per_page )->get(TBL_OFFLINE_PAYMENT)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_OFFLINE_PAYMENT." WHERE {$cond} AND status LIKE 'approved' ORDER BY create_date DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function online_payment(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "create_date >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "create_date <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('type IN("paypal","ccavenue")')->get(TBL_TRANSACTION)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_TRANSACTION." WHERE $cond AND type IN('paypal','ccavenue')")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/online_payment/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond){
			$records = $this->db->where('type IN("paypal","ccavenue")')->order_by('crDate','desc')->limit($records_per_page, $per_page )->get(TBL_TRANSACTION)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_TRANSACTION." WHERE {$cond} AND type IN('paypal','ccavenue') ORDER BY crDate DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function cod_pending(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "created_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "created_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('status NOT IN ("completed","cancelled")')->get(TBL_COD)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_COD." WHERE $cond AND status NOT IN ('completed','cancelled')")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/cod/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond){
			$records = $this->db->where('status NOT IN ("completed","cancelled")')->order_by('created_time','desc')->limit($records_per_page, $per_page )->get(TBL_COD)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_COD." WHERE {$cond} AND status NOT IN ('completed','cancelled') ORDER BY created_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function cod_completed(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "created_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "created_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('status LIKE "completed"')->get(TBL_COD)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_COD." WHERE $cond AND status LIKE 'completed'")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/cod/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond){
			$records = $this->db->where('status LIKE "completed"')->order_by('created_time','desc')->limit($records_per_page, $per_page )->get(TBL_COD)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_COD." WHERE {$cond} AND status LIKE 'completed' ORDER BY created_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function cod_cancelled(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "created_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "created_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = " OR user_id =".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			$cond = "(". (string) implode(' AND ', $tempArray2) .")".$cond2 ; 
		}
	 
		if(! $cond){
			$total = count( $this->db->where('status LIKE "cancelled"')->get(TBL_COD)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_COD." WHERE $cond AND status LIKE 'cancelled'")->result() );
		}	
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/payment/cod/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		if(! $cond){
			$records = $this->db->where('status LIKE "cancelled"')->order_by('created_time','desc')->limit($records_per_page, $per_page )->get(TBL_COD)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_COD." WHERE {$cond} AND status LIKE 'cancelled' ORDER BY created_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}					
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function approve_cod_item(){
		$COD = $this->cod_data_entity->init('id', $_POST['id']);
		$COD->setStatus('approved');
		$COD->update();
		echo 'ok';
		exit();
	}
	
	function cancel_cod_item(){
		$COD = $this->cod_data_entity->init('id', $_POST['id']);
		$COD->setStatus('cancelled');
		$COD->update();
		echo 'ok';
		exit();
	}
	
	function dispatch_cod_item(){
		$COD = $this->cod_data_entity->init('id', $_POST['id']);
		$COD->setStatus('dispatched');
		$COD->update();
		$this->email_sender->_sendEmailToUser_notifyCODdispatched($COD->getId());
		echo 'ok';
		exit();
	}
	
	function approve_payment(){
		$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id', $_POST['id']);
		$USER = $this->user_entity->init('id',$OFFLINE_PAYMENT->getUserId());
		
		$USER->setCoin($USER->getCoin() + $OFFLINE_PAYMENT->getCoin());
		$USER->update();
		
		$OFFLINE_PAYMENT->setStatus('approved');
		$OFFLINE_PAYMENT->update();
		
		$transactionId = $this->transaction_entity->createNewEntity();
		$TRANSACTION = $this->transaction_entity->init('id', $transactionId);
		$TRANSACTION->setUserId($USER->getId());
		$TRANSACTION->setPackageId($OFFLINE_PAYMENT->getPackageId());
		$TRANSACTION->setType($OFFLINE_PAYMENT->getPaymentType());
		$TRANSACTION->update();
		
		echo 'ok';
		exit();
	}
	
	function delete_payment(){
		$this->db->where('id',$_POST['id'])->delete(TBL_OFFLINE_PAYMENT);
		echo 'ok';
		exit();
	}
	
	function edit_payment_submit(){
		$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id',$_POST['id']);
		if($_POST['payment_type'] == 'bank_deposit'){
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('bank_name','')) ){
				echo "Bank Name must not empty";exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('bank_branch','')) ){
				echo "Bank Branch must not empty";exit();
			}
			$deposited_date =  convertToTimeStampFromFormat($_POST['deposited_date'], 'Y/m/d');
			if($deposited_date == 0 ){
				echo "Date is not valid.";exit();
			}
			$OFFLINE_PAYMENT->setCoin($_POST['coin']);
			$OFFLINE_PAYMENT->setBankName($this->input->post('bank_name',''));
			$OFFLINE_PAYMENT->setBankBranch($this->input->post('bank_branch',''));
			$OFFLINE_PAYMENT->setDepositedDate($deposited_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['payment_type'] == 'neft_transfer'){
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_ac_name','')) ){
				echo "Remitter AC Name is not valid.";exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_ac_number','')) ){
				echo "Remitter AC Number is not valid.";exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_bank_name','')) ){
				echo "Remitter Bank Name is not valid.";exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('transfer_time','')) ){
				echo "Transfer Time is not valid.";exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('transaction_id','')) ){
				echo "Transaction Id is not valid.";exit();
			}
			$transfer_date =  convertToTimeStampFromFormat($_POST['transfer_date'], 'Y/m/d');
			if($transfer_date == 0 ){
				echo "Transfer Date is not valid.";exit();
			}
			$OFFLINE_PAYMENT->setCoin($_POST['coin']);
			$OFFLINE_PAYMENT->setRemitterAcName($this->input->post('remitter_ac_name',''));
			$OFFLINE_PAYMENT->setRemitterAcNumber($this->input->post('remitter_ac_number',''));
			$OFFLINE_PAYMENT->setRemitterBankName($this->input->post('remitter_bank_name',''));
			$OFFLINE_PAYMENT->setTransferDate($transfer_date);
			$OFFLINE_PAYMENT->setTransferTime($this->input->post('transfer_time',''));
			$OFFLINE_PAYMENT->setTransactionId($this->input->post('transaction_id',''));
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['payment_type'] == 'dd'){
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('dd_no','')) ){
				echo "DD No. is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('dd_bank_name','')) ){
				echo "DD Bank Name is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_address','')) ){
				echo "Remitter Address is not valid.";
				exit();
			}
			
			$dd_date =  convertToTimeStampFromFormat($_POST['dd_date'], 'Y/m/d');
			if($dd_date == 0 ){
				echo "DD Date is not valid.";
				exit();
			}
			$OFFLINE_PAYMENT->setCoin($_POST['coin']);
			$OFFLINE_PAYMENT->setDdNo($this->input->post('dd_no',''));
			$OFFLINE_PAYMENT->setDdBankName($this->input->post('dd_bank_name',''));
			$OFFLINE_PAYMENT->setRemitterAddress($this->input->post('remitter_address',''));
			$OFFLINE_PAYMENT->setDdDate($dd_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['payment_type'] == 'emoney_order'){
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('emoney_order_no','')) ){
				echo "E-money Order No. is not valid.";
				exit();
			}
			if( $this->phpvalidator->is_empty($this->input->post('emoney_remitter_address','')) ){
				echo "E-money Remitter Address is not valid.";
				exit();
			}
			
			$emoney_order_date =  convertToTimeStampFromFormat($_POST['emoney_order_date'], 'Y/m/d');
			if($emoney_order_date == 0 ){
				echo "E-money Order date is not valid.";
				exit();
			}	
			$OFFLINE_PAYMENT->setEmoneyOrderNo($this->input->post('emoney_order_no',''));
			$OFFLINE_PAYMENT->setEmoneyRemitterAddress($this->input->post('emoney_remitter_address',''));
			$OFFLINE_PAYMENT->setEmoneyOrderDate($emoney_order_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
	}
	
	
	
// end class
}	